package com.itheima.tlias84.controller;

import com.itheima.tlias84.entity.Student;
import com.itheima.tlias84.service.StudentService;
import com.itheima.tlias84.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @GetMapping
    public ResultUtil selectAllStudents() {
        List<Student> students = studentService.selectAllStudents();
        return ResultUtil.success(students);
    }
    @PostMapping("/insert")
    public ResultUtil insertStudent(@RequestBody  Student student){
        int flag = studentService.insertStudent(student);
        return flag==0? ResultUtil.fail() :ResultUtil.success();
    }

    @DeleteMapping("/delete")
    public ResultUtil deleteStudentById(@RequestParam  Integer id) {
        int flag = studentService.deleteStudentById(id);
        return flag==0? ResultUtil.fail() :ResultUtil.success();
    }

    @PutMapping("/update")
    public ResultUtil updateStudentById(@RequestBody Student student) {
        int flag = studentService.updateStudentById(student);
        return  flag==0? ResultUtil.fail() :ResultUtil.success();
    }
}
